//
//  HELNavigationContrller.m
//  MobileClinicalOnline
//
//  Created by wsj2015 on 16/7/12.
//  Copyright © 2016年 陈武琦. All rights reserved.
//

#import "BTNavigationContrller.h"
#import "UIImage+SQExtension.h"
#import "UIViewController+BackButtonHandler.h"

@interface BTNavigationContrller ()<UIGestureRecognizerDelegate>




@end

@implementation BTNavigationContrller

+ (void)load {

    //颜色转图片
    CGRect rect = CGRectMake(0, 0, 1, 1);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [[UIColor whiteColor] CGColor]);
    CGContextFillRect(context, rect);
    
    UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    
    UINavigationBar *navBar = [UINavigationBar appearanceWhenContainedIn:[self class], nil];
    [navBar setBackgroundImage:[UIImage imageNamed:@"navitationbar"] forBarMetrics:UIBarMetricsDefault];
    
    //self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
     navBar.tintColor = [UIColor whiteColor];
    
    /* 4.设置导航条标题字体颜色以及大小*/
    
    [navBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18], NSForegroundColorAttributeName:[UIColor whiteColor]}];
   
   
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationBar.translucent = NO;
    
    self.navigationBar.shadowImage = [UIImage new];
    //自定义分割线  因为原生分割线  在无导航 返回有导航 页面时候 会消失
    UIView *view =[UIView new];
    view.backgroundColor = kBorderColor;
    view.frame = CGRectMake(0, 44- kOnePX, kScreenWidth, kOnePX);
    [self.navigationBar addSubview:view];
}


- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    
    viewController.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleDone target:nil action:nil];
//    WSJLog(@"%@",self.viewControllers);
    if (self.viewControllers.count != 0) {
        viewController.hidesBottomBarWhenPushed = YES;
    }
    
    [super pushViewController:viewController animated:animated];
}


- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem*)item {
    
    if([self.viewControllers count] < [navigationBar.items count]) {
        return YES;
    }
    
    BOOL shouldPop = YES;
    UIViewController* vc = [self topViewController];
    if([vc respondsToSelector:@selector(navigationShouldPopOnBackButton)]) {
        shouldPop = [vc navigationShouldPopOnBackButton];
    }
    
    if(shouldPop) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [self popViewControllerAnimated:YES];
        });
    } else {
        // Workaround for iOS7.1. Thanks to @boliva - http://stackoverflow.com/posts/comments /34452906
        for(UIView *subview in [navigationBar subviews]) {
            if(subview.alpha < 1.) {
                [UIView animateWithDuration:.25 animations:^{
                    subview.alpha = 1.;
                }];
            }
        }
    }
    
    return NO;
}

//导航控制器内的所有vc 只支持竖屏

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    if (isIOS8) {
        return UIInterfaceOrientationMaskAll;
    }
    return UIInterfaceOrientationMaskPortrait ;
}

@end
